package org.yoyo.classroom.controller;

import com.github.pagehelper.PageInfo;
import org.yoyo.classroom.domain.po.BookingDTO;
import org.yoyo.classroom.domain.po.BookingOrder;
import org.yoyo.classroom.domain.po.ValidationResult;
import org.yoyo.classroom.result.Result;
import org.yoyo.classroom.service.BookingOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yoyo.classroom.service.BookingValidationService;
import org.yoyo.classroom.util.BookingConverter;
import org.yoyo.classroom.utils.JwtTokenUtil;

import java.util.List;

/**
 * 预约订单控制器
 */
@RestController
@RequestMapping("/booking/booking-order")
public class BookingOrderController {

    @Autowired
    private BookingOrderService bookingOrderService;
    @Autowired
    private BookingValidationService validationService;
    /**
     * 根据ID查询预约订单
     * @param id 预约订单ID
     * @return 包含预约订单对象的结果
     */
    @GetMapping("/{id}")
    public Result<BookingOrder> getBookingOrderById(@PathVariable Long id) {
        BookingOrder bookingOrder = bookingOrderService.getBookingOrderById(id);
        if (bookingOrder != null) {
            return Result.success(bookingOrder);
        } else {
            return Result.error("Booking order not found");
        }
    }

    /**
     * 查询所有预约订单
     * @return 包含预约订单列表的结果
     */
    @GetMapping
    public Result<List<BookingOrder>> getBookingOrders() {

        List<BookingOrder> bookingOrders = bookingOrderService.getBookingOrders();
        if (bookingOrders != null && !bookingOrders.isEmpty()) {
            return Result.success(bookingOrders);
        } else {
            return Result.error("No booking orders found");
        }
    }
    @GetMapping("/findOrderByUser")
    public Result<PageInfo<BookingOrder>> findOrderByUser(
            @RequestHeader(value = "Authorization", required = false) String authorizationHeader,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String bookingDate,
            @RequestParam(required = false) Integer status
    ) {
        Long userId = new JwtTokenUtil().getUserIdFromToken(authorizationHeader.substring(7));
        PageInfo<BookingOrder> pageInfo = bookingOrderService.findOrderByUserIdWithPage(userId, pageNum, pageSize, bookingDate, status);

        //List<BookingOrder> bookingOrders = bookingOrderService.findOrderByUserId(userId);

            return Result.success(pageInfo);

    }

    /**
     * 插入预约订单
     * @param bookingOrder 预约订单对象
     * @return 包含影响行数的结果
     */
    @PostMapping("/")
    public Result<Integer> addBookingOrder(@RequestBody BookingOrder bookingOrder) {
        int affectedRows = bookingOrderService.addBookingOrder(bookingOrder);
        return Result.success(affectedRows);
    }

    /**
     * 更新预约订单
     * @param bookingOrder 预约订单对象
     * @return 包含影响行数的结果
     */
    @PutMapping("/")
    public Result<Integer> updateBookingOrder(@RequestBody BookingOrder bookingOrder) {
        int affectedRows = bookingOrderService.updateBookingOrder(bookingOrder);
        return Result.success(affectedRows);
    }

    /**
     * 删除预约订单（逻辑删除）
     * @param id 预约订单ID
     * @return 包含影响行数的结果
     */
    @DeleteMapping("/{id}")
    public Result<Integer> deleteBookingOrder(@PathVariable Long id) {
        int affectedRows = bookingOrderService.deleteBookingOrder(id);
        return Result.success(affectedRows);
    }


}
